package com.yuyou.fn.platform.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.platform.entity.UserNotifyConfig;
import com.yuyou.fn.platform.mapper.UserNotifyConfigMapper;
import com.yuyou.fn.platform.service.IUserNotifyConfigService;
import com.yuyou.fn.platform.vo.UserNotifyConfigSaveVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * UserNotifyConfig 表数据服务层接口实现类
 *
 */
@Service
public class UserNotifyConfigServiceImpl extends SuperServiceImpl<UserNotifyConfigMapper, UserNotifyConfig,Long> implements IUserNotifyConfigService {

    @Override
    public void saveOrUpdateNotifyConfig(Long userId, UserNotifyConfigSaveVo userNotifyConfigSaveVo) {
        UserNotifyConfig userNotifyConfig=this.findNotifyConfigByUserIdWay(userId,userNotifyConfigSaveVo.getWay());

        if (userNotifyConfig!=null){
            BeanUtils.copyProperties(userNotifyConfig,userNotifyConfigSaveVo);
            userNotifyConfig.setUpdateTime(TimeUtils.now());
            userNotifyConfig.setState(1);
            this.updateSelectiveById(userNotifyConfig);
        }else {
            userNotifyConfig=new UserNotifyConfig();
            BeanUtils.copyProperties(userNotifyConfig,userNotifyConfigSaveVo);
            userNotifyConfig.setUserId(userId);
            userNotifyConfig.setState(1);
            userNotifyConfig.setCreateTime(TimeUtils.now());
            userNotifyConfig.setUpdateTime(TimeUtils.now());
            this.insertSelective(userNotifyConfig);
        }
    }

    @Override
    public UserNotifyConfig findNotifyConfigByUserIdWay(long userId, int way) {
        Example<UserNotifyConfig> userNotifyConfigExample=UserNotifyConfig.createExample();
        userNotifyConfigExample.createCriteria().andEqualTo("userId",userId).andEqualTo("way",way);
        return this.findOneByExample(userNotifyConfigExample);
    }

    @Override
    public List<UserNotifyConfig> findNotifyConfigByWay(int way) {
        Example<UserNotifyConfig> userNotifyConfigExample=UserNotifyConfig.createExample();
        userNotifyConfigExample.createCriteria().andEqualTo("way",way).andEqualTo("state",1);
        return this.findListByExample(userNotifyConfigExample);
    }

    @Override
    public void updateEmailNotifyConfigState(long userId, int state) {
        Example<UserNotifyConfig> userNotifyConfigExample=UserNotifyConfig.createExample();
        Example.Criteria criteria=userNotifyConfigExample.createCriteria();
        criteria.andEqualTo("userId",userId).andEqualTo("way",0);
        UserNotifyConfig update=new UserNotifyConfig();
        update.setState(state);
        this.updateSelectiveByExample(update,userNotifyConfigExample);
    }
}